package com.cci.kangdao.aop;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 过滤器
 */
@Component
@WebFilter(filterName="handleFilter",urlPatterns="/*")
public class HandleFilter extends OncePerRequestFilter {
    private final static Log log = LogFactory.getLog(HandleFilter.class);
    // 所有需要解密字段集合
    private String[] fieldArray = new String[]{"mobile","smsCode","phone","province","city","verifyCode","email","cellPhone","account","CustomerPhone","telephone"};

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String contentType = request.getContentType();
        //body形式（json）
        if (StringUtils.isNotBlank(contentType)
                && contentType.contains(MediaType.APPLICATION_JSON_VALUE)) {
            //获取request的body参数
            String postContent = getBody(request);
            if (StringUtils.isNotEmpty(postContent)) {
                BodyRequestWrapper bodyRequestWrapper = new BodyRequestWrapper(request, postContent);
                bodyRequestWrapper.getParameterDecrpty(fieldArray);
                request = bodyRequestWrapper;
            }
            //form表单形式
        } else if (!request.getParameterMap().isEmpty()) {
            ModifyParametersWrapper mParametersWrapper = new ModifyParametersWrapper(request);
            mParametersWrapper.getParameterDecrpty(fieldArray);
            request = mParametersWrapper;
        }
        filterChain.doFilter(request, response);
    }

    //获取Request的body数据
    private String getBody(ServletRequest request) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuilder.toString();
    }

}

